package Utility;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import DataBase.Assignment;
import DataBase.AssignmentManager;
import DataBase.Backup;
import DataBase.BackupManager;
import DataBase.Datasource;
import DataBase.DatasourceManager;

public class AssignmentHandler extends Thread {
	private Assignment asmt;
	private Datasource datasource;
	public AssignmentHandler(Assignment asmt) {
		super();
		// TODO Auto-generated constructor stub
		this.asmt = asmt;
		this.datasource = DatasourceManager.select(asmt.getDs_ID());
	}
	
	
	public void run(){
		System.out.println("AssignmentHandler : run 任务" + datasource + asmt);
		
		FTP ftp = new FTP();
	    try {
			ftp.connectServer(this.datasource.getDs_IP(),this.asmt.getAsmt_ftpname(),this.asmt.getAsmt_ftppwd(),"");
			 try {
					ftp.download(this.asmt.getAsmt_path(),getLocation());
					this.asmt.setAsmt_state("开启"); 
					this.asmt.updateNext();
					Backup bc = new Backup();
					bc.setBackup_date(this.asmt.getfulltime());
					bc.setds_ID(this.datasource.getDs_ID());
					bc.setBackup_source(this.asmt.getAsmt_path());
					BackupManager.insert(bc);
				} catch (Exception e) {
					// TODO Auto-generated catch block
				    this.asmt.setAsmt_state("路径错误");
					e.printStackTrace();
				}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			this.asmt.setAsmt_state("连接错误"); 
			e.printStackTrace();
		}
	    AssignmentManager.update(this.asmt);
	   }
	
	public String getLocation(){
		String path  = "d:/TYZY/" + this.datasource.getDs_hostname() + "/";
		MakeDir dir = new MakeDir();
		dir.makedirectory(path);
		String filename = this.asmt.getfulltime() + " " + this.asmt.getAsmt_path();
		return path + filename;
	}
	
}